using Furion.DatabaseAccessor;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

using Tool;

namespace Abc.Core.Entities
{
    /// <summary>
    /// 用户表
    /// </summary>
    [Comment("用户表")]
    [TablePrefix("Web.")]
    public class User : FullEntity, IEntityTypeBuilder<User>, IEntitySeedData<User>
    {
        /// <summary>
        /// 用户名(必填)
        /// </summary>
        [Comment("用户名")]
        public string Account { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Comment("密码")]
        public string Pwd { get; set; }

        /// <summary>
        /// 账户类型
        /// </summary>
        [Comment("账户类型")]
        public UserType UserType { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        [Comment("是否启用")]
        public bool IsEnable { get; set; }

        /// <summary>
        /// 是否测试
        /// </summary>
        [Comment("是否测试")]
        public bool IsTest { get; set; }

        /// <summary>
        /// 昵称
        /// </summary>
        [Comment("昵称")]
        public string Nick { get; set; }

        /// <summary>
        /// 邮箱(必填)
        /// </summary>
        [Comment("邮箱")]
        public string Email { get; set; }

        /// <summary>
        /// 省份
        /// </summary>
        [Comment("省份")]
        public string Province { get; set; }

        /// <summary>
        /// 城市
        /// </summary>
        [Comment("城市")]
        public string City { get; set; }

        /// <summary>
        /// 镇
        /// </summary>
        [Comment("镇")]
        public string Town { get; set; }

        /// <summary>
        /// 详细地址
        /// </summary>
        [Comment("详细地址")]
        public string Address { get; set; }

        /// <summary>
        /// 联系电话
        /// </summary>
        [Comment("联系电话")]
        public string Phone { get; set; }

        /// <summary>
        /// 办公电话
        /// </summary>
        [Comment("办公电话")]
        public string Tel { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [Comment("备注")]
        public string Remark { get; set; }

        /// <summary>
        /// 最后登录时间
        /// </summary>
        [Comment("最后登录时间")]
        public DateTimeOffset? LastLoginTime { get; set; }

        /// <summary>
        /// 最后登录IP
        /// </summary>
        [Comment("最后登录IP")]
        public string LastLoginIp { get; set; }

        /// <summary>
        /// 最后登录用户浏览器信息
        /// </summary>
        [Comment("最后登录用户浏览器信息")]
        public string LastLoginUserAgent { get; set; }

        /// <summary>
        /// 多对多
        /// </summary>
        public ICollection<Role> Roles { get; set; }

        public List<UserRole> UserRoles { get; set; }

        public void Configure(EntityTypeBuilder<User> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            // HasIndex 拆分写的目的是，因为mysql key有长度限制
            entityBuilder.HasIndex(o => new { o.Account, o.UserType });
            entityBuilder.HasIndex(o => new { o.Email, o.Phone });
            entityBuilder.HasIndex(o => new { o.Tel, o.Nick });
            entityBuilder.HasMany(o => o.Roles).WithMany(o => o.Users).UsingEntity<UserRole>(
                u => u.HasOne(c => c.Role).WithMany(c => c.UserRoles).HasForeignKey(c => c.RoleId)
                , u => u.HasOne(c => c.User).WithMany(c => c.UserRoles).HasForeignKey(c => c.UserId)
                , u =>
                {
                    u.HasKey(c => new { c.UserId, c.RoleId });
                    //u.HasData(new {AdminId=1,RoleId=1 })//数据种子
                    //u.Property(pt => pt.CreationTime).HasDefaultValueSql("CURRENT_TIMESTAMP");//每种数据库支持不一样
                }
            );
        }

        public IEnumerable<User> HasData(DbContext dbContext, Type dbContextLocator)
        {
            var devpwd = "dev123654".ToPBKDF2();
            var pwd = "admin123654".ToPBKDF2();
            var pwd2 = "test123987".ToPBKDF2();
            return new List<User>()
            {
                new User(){ Id=1 ,Account= "developer",Nick="超级管理员",Pwd= devpwd,UserType = UserType.超级管理员,CreationTime=DateTimeOffset.Now,CreatorUserId=0,CreatorUserName=""},
                new User(){ Id=2 ,Account= "administrator",Nick="超级管理员",Pwd= pwd,UserType = UserType.超级管理员,CreationTime=DateTimeOffset.Now,CreatorUserId=0,CreatorUserName=""},
                new User(){ Id=3 ,Account= "testuser",Nick="测试用户",Pwd= pwd2,UserType = UserType.普通用户,CreationTime=DateTimeOffset.Now,CreatorUserId=0,CreatorUserName=""}
            };
        }
    }

    public enum UserType
    {
        普通用户 = 0,
        超级管理员 = 99,
        管理员 = 100,
        系统维护 = 101,
    }
}